//
//  YDLoginView.m
//  YDRACLearn
//
//  Created by rowena on 2019/1/10.
//  Copyright © 2019年 wuyezhiguhun. All rights reserved.
//

#import "YDLoginView.h"

@interface YDLoginView()

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *userNameLabel;
@property (nonatomic, strong) UILabel *passwordLabel;
@property (nonatomic, strong) UIView *userNameView;
@property (nonatomic, strong) UIView *passwordView;

@end

@implementation YDLoginView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addAllViews];
        [self setAllViewsLayout];
    }
    return self;
}

#pragma mark ==== 添加View ====
- (void)addAllViews {
    [self addSubview:self.iconImageView];
    [self addSubview:self.userNameLabel];
    [self addSubview:self.passwordLabel];
    [self addSubview:self.userNameView];
    [self addSubview:self.passwordView];
    [self addSubview:self.userNameField];
    [self addSubview:self.passwordField];
    [self addSubview:self.loginButton];
}

#pragma mark ==== 设置 UI ====
- (void)setAllViewsLayout {
    [self.iconImageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:80];
    [self.iconImageView autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.iconImageView autoSetDimension:ALDimensionWidth toSize:120];
    [self.iconImageView autoSetDimension:ALDimensionHeight toSize:120];
    
    [self.userNameLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.iconImageView withOffset:80];
    [self.userNameLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:30];
    [self.userNameLabel autoSetDimension:ALDimensionHeight toSize:20];
    [self.userNameLabel autoSetDimension:ALDimensionWidth toSize:80];
    
    [self.passwordLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.userNameLabel withOffset:45];
    [self.passwordLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20];
    [self.passwordLabel autoSetDimension:ALDimensionHeight toSize:30];
    [self.passwordLabel autoSetDimension:ALDimensionWidth toSize:80];
    
    [self.userNameView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.userNameLabel withOffset:0];
    [self.userNameView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.userNameLabel withOffset:10];
    [self.userNameView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:40];
    [self.userNameView autoSetDimension:ALDimensionHeight toSize:1.0];
    
    [self.passwordView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.passwordLabel withOffset:0];
    [self.passwordView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.passwordLabel withOffset:10];
    [self.passwordView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:40];
    [self.passwordView autoSetDimension:ALDimensionHeight toSize:1.0];
    
    [self.userNameField autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.userNameLabel withOffset:10];
    [self.userNameField autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:40];
    [self.userNameField autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:self.userNameView];
    [self.userNameField autoSetDimension:ALDimensionHeight toSize:30];
    
    [self.passwordField autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.passwordLabel withOffset:10];
    [self.passwordField autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:40];
    [self.passwordField autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:self.passwordView];
    [self.passwordField autoSetDimension:ALDimensionHeight toSize:30];
    
    [self.loginButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.passwordView withOffset:60];
    [self.loginButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:40];
    [self.loginButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:40];
    [self.loginButton autoSetDimension:ALDimensionHeight toSize:40];
}

#pragma mark ==== get 函数 ====
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_icon"]];
        _iconImageView.layer.cornerRadius = 60.0;
        _iconImageView.layer.masksToBounds = YES;
    }
    return _iconImageView;
}
- (UILabel *)userNameLabel {
    if (!_userNameLabel) {
        _userNameLabel = [[UILabel alloc] init];
        _userNameLabel.backgroundColor = [UIColor clearColor];
        _userNameLabel.textColor = [UIColor whiteColor];
        _userNameLabel.font = [UIFont systemFontOfSize:15];
        _userNameLabel.textAlignment = NSTextAlignmentRight;
        _userNameLabel.text = @"用户名:";
    }
    return _userNameLabel;
}
- (UILabel *)passwordLabel {
    if (!_passwordLabel) {
        _passwordLabel = [[UILabel alloc] init];
        _passwordLabel.backgroundColor = [UIColor clearColor];
        _passwordLabel.textColor = [UIColor whiteColor];
        _passwordLabel.font = [UIFont systemFontOfSize:15];
        _passwordLabel.textAlignment = NSTextAlignmentRight;
        _passwordLabel.text = @"密    码:";
    }
    return _passwordLabel;
}
- (UIView *)userNameView {
    if (!_userNameView) {
        _userNameView = [[UIView alloc] init];
        _userNameView.backgroundColor = [UIColor whiteColor];
    }
    return _userNameView;
}
- (UIView *)passwordView {
    if (!_passwordView) {
        _passwordView = [[UIView alloc] init];
        _passwordView.backgroundColor = [UIColor whiteColor];
    }
    return _passwordView;
}
/** 创建用户名输入框 */
- (UITextField *)userNameField {
    if (!_userNameField) {
        _userNameField = [[UITextField alloc] init];
        _userNameField.backgroundColor = [UIColor clearColor];
        _userNameField.font = [UIFont systemFontOfSize:15];
        _userNameField.textColor = [UIColor whiteColor];
        _userNameField.textAlignment = NSTextAlignmentLeft;
        _userNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _userNameField;
}

/** 创建密码输入框 */
- (UITextField *)passwordField {
    if (!_passwordField) {
        _passwordField = [[UITextField alloc] init];
        _passwordField.backgroundColor = [UIColor clearColor];
        _passwordField.font = [UIFont systemFontOfSize:15];
        _passwordField.textColor = [UIColor whiteColor];
        _passwordField.textAlignment = NSTextAlignmentLeft;
        _passwordField.enablesReturnKeyAutomatically = YES;
        _passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _passwordField;
}

/** 创建登录点击按钮 */
- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginButton.backgroundColor = [UIColor orangeColor];
        [_loginButton setTitle:@"登        录" forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
        _loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        _loginButton.layer.cornerRadius = 5.0;
        _loginButton.layer.masksToBounds = YES;
    }
    return _loginButton;
}

/** 创建账号 */
- (UIButton *)registerButton {
    if (!_registerButton) {
        _registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _registerButton.backgroundColor = [UIColor orangeColor];
        [_registerButton setTitle:@"创建账号" forState:UIControlStateNormal];
        [_registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _registerButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        _registerButton.layer.cornerRadius = 5.0;
        _registerButton.layer.masksToBounds = YES;
    }
    return _registerButton;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
